package com.android.jay.lib_image_loader.app;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;


import android.view.ViewGroup;
import android.widget.ImageView;
import android.graphics.drawable.Drawable;
import android.widget.RemoteViews;

import com.android.jay.lib_image_loader.R;
import com.android.jay.lib_image_loader.image.Utils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.target.NotificationTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;


/**
 * Created by wzp on 2019/12/30 0030.
 * <p>
 * 描述  ：对外提供加载图片的不同加载方法
 */
public class ImageLoaderManager {


    private ImageLoaderManager() {

    }

    /**
     * 内部类构造单例的模式
     **/
    private static class SingletonHolder {
        private static ImageLoaderManager instance = new ImageLoaderManager();
    }

    public static ImageLoaderManager getInstance() {
        return SingletonHolder.instance;
    }


    /**
     * 为imageView加载图片
     **/
    public void displayImageForView(ImageView view, String url) {
        Glide.with(view.getContext())
                .load(url)
                .apply(initCommonRequestOption())
                .transition(withCrossFade())
                .into(view);
    }

    /**
     * 为imageview 加载圆形图片
     **/
    public void displayImageForCircle(ImageView view, String url) {
        Glide.with(view.getContext())
                .asBitmap()
                .load(url)
                .apply(initCommonRequestOption())
                .into(new BitmapImageViewTarget(view) {
                    //将imageview包装成target
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable drawable =
                                RoundedBitmapDrawableFactory.create(view.getResources(), resource);
                        drawable.setCircular(true);
                        view.setImageDrawable(drawable);
                    }
                });
    }

    /**
     * 为viewgroup 设置背景被模糊处理
     * **/
    public void displayImageForViewGroup(final ViewGroup group, String url, final boolean doblur) {

        Glide.with(group.getContext())
                .asBitmap()
                .load(url)
                .apply(initCommonRequestOption())
                .into(new SimpleTarget<Bitmap>() {
                    @SuppressLint("CheckResult")
                    @Override
                    public void onResourceReady(@NonNull final Bitmap resource,
                                                @Nullable Transition<? super Bitmap> transition) {
                        /**
                         * 在主线程  rxjava
                         * **/
                        final Bitmap bitmap = resource;
                        Observable.just(resource).map(new Function<Bitmap, Drawable>() {
                            @Override
                            public Drawable apply(Bitmap bitmap) throws Exception {
                                //将bitmap 进行模糊处理
                                Drawable drawable =
                                        new BitmapDrawable(doblur ? Utils.doBlur(resource, 100,
                                                true) : resource);
                                return drawable;
                            }
                        }).subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Consumer<Drawable>() {
                                    @Override
                                    public void accept(Drawable drawable) throws Exception {
                                        group.setBackground(drawable);
                                    }
                                });
                    }
                });
    }


    /**
     * 为notification中的id控件加载图片 like weiget 也同样这样写
     * @param id              这个id 要加载图片的控件ID
     * @param NOTIFICATION_ID 这个是 notification id
     *
     **/
    public void displayImageForNotification(Context context,
                                            RemoteViews remoteViews,
                                            int id, Notification notification,
                                            int NOTIFICATION_ID, String url) {
        this.displayImageForTarget(context,
                initNotificationTarget(context, remoteViews, id,
                        notification, NOTIFICATION_ID), url);
    }


    /**
     * 构造一个notificationtarget
     **/
    private NotificationTarget initNotificationTarget(Context context,
                                                      RemoteViews remoteViews,
                                                      int id, Notification notification,
                                                      int NOTIFICATION_ID) {
        NotificationTarget target = new NotificationTarget(context, id, remoteViews, notification, NOTIFICATION_ID);
        return target;
    }

    /**
     * 为非view加载图片
     **/

    private void displayImageForTarget(Context context, Target target, String url) {
        Glide.with(context)
                .asBitmap()
                .load(url)
                .apply(initCommonRequestOption())
                .transition(BitmapTransitionOptions.withCrossFade())
                .into(target);
    }

    @SuppressLint("CheckResult")
    private RequestOptions initCommonRequestOption() {
        RequestOptions options = new RequestOptions();
        options.placeholder(R.mipmap.b4y)
                .error(R.mipmap.b4y)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)//缓存策略
                .skipMemoryCache(false)//使用内存缓存
                .priority(Priority.NORMAL);
        return options;

    }


}
